import socketserver


class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        try:
            while True:
                # 接受消息
                self.data = self.request.recv(1024)
                # 发送消息
                self.request.sendall("发送内容")
        except Exception:
            print(self.client_address, "连接断开")
        finally:
            self.request.close()

    def setup(self):
        print("before handle,连接建立：", self.client_address)

    def finish(self):
        print("finish run  after handle")


if __name__ == "__main__":
    server = socketserver.ThreadingTCPServer(("localhost", 8899), MyTCPHandler)
    server.serve_forever()
